package com.thread;

public class Bank {
    private int account = 20000;//账户余额

    //取钱
    public synchronized boolean getMoney(int money) {
        int account = getAccount();
        if (account >= money) {
            account -= money;
            Thread.yield();//放弃当前CPU时间片
            saveAccount(account);
            return true;
        }
        return false;
    }

    //保存账户余额
    public void saveAccount(int account) {
        //这里的this.account是当前对象的account，当前对象是Bank对象
        //account是方法的参数
        this.account = account;
    }

    //获取账户余额
    public int getAccount() {
        return account;
    }

}
